package com.jdenergy.emindHome.ble_plugin;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import com.jdenergy.emindHome.ble_plugin.EmindHomeBLEPlugin;

import com.igen.bleconfig.BLEConfigurationManager;
import com.igen.bleconfig.BLEConfigurationResultCallback;
import com.igen.bleconfig.constant.ResultCode;
import com.igen.bleconfig.log.BLEDebugLog;

import java.util.ArrayList; // 引入 ArrayList 类
import java.util.List;
import java.util.logging.LogManager;

/** BlePlugin */
public class BlePlugin implements FlutterPlugin, MethodCallHandler, EmindHomeBLEPlugin.EmindHomeBlePlugin {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  private Context newContext ;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "ble_plugin");
    channel.setMethodCallHandler(this);
    newContext = flutterPluginBinding.getApplicationContext();
    EmindHomeBLEPlugin.EmindHomeBlePlugin.setUp(flutterPluginBinding.getBinaryMessenger(), this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  /**初始化, 开始设置 Appkey */
  @Override
  public void initAndSetAppKey(@NonNull String appKey, @NonNull EmindHomeBLEPlugin.Result<Boolean> result) {

    BLEConfigurationManager.INSTANCE.initBLEConfigurationManager(newContext, appKey, new BLEConfigurationResultCallback() {
      @Override
      public void onResult(int code) {
        if (code == ResultCode.BLE_CODE_TYPE_CHECK_OK) {
          // TODO:  Authentication succeeded
          result.success(true);
        } else if (code == ResultCode.BLE_CODE_TYPE_CHECK_FAILED) {
          // TODO: Authentication failed
          result.success(false);
        }
//   Log.d("initAndSetAppKey", "onResult: "+ code);
      }
    });
  }

  /**开始配网 */
  @Override
  public void startConfiguration(@NonNull EmindHomeBLEPlugin.StartConfigurationRequest request, @NonNull EmindHomeBLEPlugin.Result<Boolean> result) {

    BLEConfigurationManager.INSTANCE.startBLEConfigurationManager(newContext,request.getSn(),request.getSsid(),request.getPassword(),new BLEConfigurationResultCallback() {
      @Override
      public void onResult(int code) {
        if (code == ResultCode.BLE_CODE_TYPE_OK) {
          // TODO:  Configuration succeeded
          result.success(true);
        } else {
          // TODO: Configuration failed
          result.success(false);
        } }
    });
  }

  /**获取最后一次配网日志 */
  @NonNull
  @Override
  public List<Object> getLogsOfLastConfiguration() {

    List<BLEDebugLog>  logs = BLEConfigurationManager.INSTANCE.getLogsOfLastConfiguration();
    EmindHomeBLEPlugin.LogMessage logMessage = new EmindHomeBLEPlugin.LogMessage();
    BLEDebugLog log = logs.get(0);
    logMessage.setMessage(log.getMessage());
    logMessage.setDateTime(log.getDate());
    //    Object[] myList;
    //    myList.push(logMessage);
    ArrayList<Object> objectName =new ArrayList<>(); // 初始化
    objectName.add(logMessage);
    return objectName;
  }
  /**退出 */
  @Override
  public void exitConfiguration() {
    BLEConfigurationManager.INSTANCE.exitBLEConfigurationManager();
  }
  /**
   * 添加日志
   * message  日志信息
   */
  @NonNull
  @Override
  public EmindHomeBLEPlugin.LogMessage addLogWithMessage(@NonNull String message)
  {
     return null;
  }
  /**获取所有的日志 */
  @NonNull
  @Override
  public List<Object> getAllLog() {
    return null;
  }
  /**获取所有的日志内容 */
  @NonNull
  @Override
  public List<Object> getAllLogMessages() {
    return null;
  }
  /**删除所有的日志 */
  @Override
  public void removeAllLogs() {}
  /**蓝牙状态改变 */
  @NonNull
  @Override
  public String getBLEBluetoothStateDidChange() {
    return null;
  }
  /**蓝牙状态改变 */
  @NonNull
  @Override
  public Long getBluetoothState() {
    return null;
  }
  /**是否初始化成功 */
  @NonNull
  @Override
  public Boolean getBLESetupFinished() {
    return null;
  }
  /**获取BLE版本 */
  @NonNull
  @Override
  public String getBLEVersion() {
    return null;
  }
}
